package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipDevAreaDef;
import cc.comac.mom.bean.EquipDevAreaDefBean;
import cc.comac.mom.dao.entity.EquipDevAreaDefDao;
import cc.comac.mom.dao.entity.EquipDevDao;
import cc.comac.mom.entity.EquipDevAreaDefEntity;
import cc.comac.mom.entity.EquipDevEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipDevAreaDefDataHandler extends ServiceImpl<EquipDevAreaDefDao, EquipDevAreaDefEntity> {

	@Autowired
	private EquipDevAreaDefDao equipDevAreaDefDao;
	
	@Autowired	
	private EquipDevDao equipDevDao;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	public ResultBean save(EquipDevAreaDef obj) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipDevAreaDefEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");
		}
		if(innerData.getDevId() == 0) {	
			throw new RuntimeException("主表信息id为空");
		}
		if(innerData.getAreaNumber() == null || "".equals(innerData.getAreaNumber())) {	
			throw new RuntimeException("部位编号为空");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("area_number",innerData.getAreaNumber());
		map.put("dev_id", innerData.getDevId());
		map.put("row_status", 1);
		List<EquipDevAreaDefEntity> selectByMap = equipDevAreaDefDao.selectByMap(map);
		if(selectByMap != null && selectByMap.size()>0) {	
			throw new RuntimeException("该部位定义信息已经存在");
		}
		baseEntityDataUtil.setBaseEntityData(innerData);
		int insert = equipDevAreaDefDao.insert(innerData);
		if(insert<1) {
			throw new RuntimeException("新增失败");
		}
		return resultBean;
	}
	
	public ResultBean update(EquipDevAreaDef obj) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipDevAreaDefEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");	
		}
		if(innerData.getDevId() == 0) {
			throw new RuntimeException("主表信息id为空");
		}
		if(innerData.getAreaNumber() == null || "".equals(innerData.getAreaNumber())) {
			throw new RuntimeException("部位编号为空");
		}
		EquipDevAreaDefEntity selectById = equipDevAreaDefDao.selectById(innerData.getId());
		if(selectById == null) {
			throw new RuntimeException("修改的数据不存在！");
		}
		if(!selectById.getAreaNumber().equals(innerData.getAreaNumber())) {
			throw new RuntimeException("部位编号不能修改");
		}
		baseEntityDataUtil.updateBaseEntityData(innerData);
		int update = equipDevAreaDefDao.updateById(innerData);
		if(update<1) {
			throw new RuntimeException("修改失败");
		}
		return resultBean;
	}
	
	public ResultBean delete(EquipDevAreaDef obj) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipDevAreaDefEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");
		}
		EquipDevAreaDefEntity selectById = equipDevAreaDefDao.selectById(innerData.getId());
		if(selectById == null) {
			throw new RuntimeException("删除的数据不存在！");
		}
		baseEntityDataUtil.deleteBaseEntityData(selectById);
		int delete = equipDevAreaDefDao.updateById(selectById);
		if(delete<1) {
			throw new RuntimeException("删除失败");
		}
		return resultBean;
	}
	
	public EquipDevAreaDefBean selectByCondition(EquipDevAreaDef obj) {
    	QueryWrapper<EquipDevAreaDefEntity> queryWrapper = new QueryWrapper<EquipDevAreaDefEntity>();
    	queryWrapper.eq("row_status",1);
    	if(obj != null && obj.getInnerData() != null) {
    		if(obj.getInnerData().getDevId() != 0) {
    			ArrayList<Long> arrayList = new ArrayList<Long>();
    			arrayList.add(obj.getInnerData().getDevId());
    			EquipDevEntity equipDevEntity = equipDevDao.selectById(obj.getInnerData().getDevId());
    			if(equipDevEntity != null) {
    				arrayList.add(equipDevEntity.getDevCategoryId());
    			}
    			queryWrapper.in("dev_id",arrayList);
    		}
    		if(obj.getInnerData().getAreaNumber()!= null && !"".equals(obj.getInnerData().getAreaNumber())) {
    			queryWrapper.like("append_name",obj.getInnerData().getAreaNumber());
    		}
    		if(obj.getInnerData().getDataSource()!= null && !"".equals(obj.getInnerData().getDataSource())) {
    			queryWrapper.like("data_source",obj.getInnerData().getDataSource());
    		}
    	}
        Page<EquipDevAreaDefEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
        IPage<EquipDevAreaDefEntity> iPage = equipDevAreaDefDao.selectPage(page, queryWrapper);
        List<EquipDevAreaDefEntity> list = iPage.getRecords();
        if(list != null && list.size()>0) {
        	long pages = iPage.getPages();
        	long total = iPage.getTotal();
        	ArrayList<EquipDevAreaDef> arrayList = new ArrayList<EquipDevAreaDef>();
        	for(EquipDevAreaDefEntity item :list) {
	        	EquipDevAreaDef equipDevAreaDef = new EquipDevAreaDef();
	        	equipDevAreaDef.setInnerData(item);
	        	arrayList.add(equipDevAreaDef);
        	}
        	EquipDevAreaDefBean EquipDevAreaDefBean = new EquipDevAreaDefBean();
        	EquipDevAreaDefBean.setList(arrayList);
        	EquipDevAreaDefBean.setPageNum((int)obj.getPageNum());
        	EquipDevAreaDefBean.setPageSize((int)obj.getPageSize());
        	EquipDevAreaDefBean.setPages((int)pages);
        	EquipDevAreaDefBean.setTotal((int)total);
        	return EquipDevAreaDefBean;
        }
		return null;
		
	}
	
	public EquipDevAreaDef getById(long id) {
		// TODO Auto-generated method stub
		EquipDevAreaDefEntity equipDevAreaDefEntity = equipDevAreaDefDao.selectById(id);
		if(equipDevAreaDefEntity != null) {
			EquipDevAreaDef equipDevAreaDef = new EquipDevAreaDef();
			equipDevAreaDef.setInnerData(equipDevAreaDefEntity);
			return equipDevAreaDef;
		}
		return null;
	}
	//设备类型id获取部位定义信息
	public List<EquipDevAreaDef> getAreaNameByEquipTypeId(long id) {
		ArrayList<Long> IdList = new ArrayList<Long>();
		IdList.add(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("row_status", 1);
		map.put("dev_category_id",id);
		List<EquipDevEntity> selectByMap = equipDevDao.selectByMap(map);
		if(selectByMap != null && selectByMap.size()>0) {
			for(EquipDevEntity item:selectByMap) {
				IdList.add(item.getId());
			}
		}
		QueryWrapper<EquipDevAreaDefEntity> queryWrapper = new QueryWrapper<EquipDevAreaDefEntity>();
		queryWrapper.eq("row_status",1);
		queryWrapper.in("dev_id", IdList);
		queryWrapper.groupBy("area_name");
		List<EquipDevAreaDefEntity> selectList = equipDevAreaDefDao.selectList(queryWrapper);
		if(selectList != null && selectList.size()>0) {
			ArrayList<EquipDevAreaDef> arrayList = new ArrayList<EquipDevAreaDef>();
			for(EquipDevAreaDefEntity item:selectList) {
				EquipDevAreaDef equipDevAreaDef = new EquipDevAreaDef();
				equipDevAreaDef.setInnerData(item);
				arrayList.add(equipDevAreaDef);
			}
			return arrayList;
		}
		return null;
		
	}
	//通过设备主数据id获取部位定义信息
	public List<EquipDevAreaDef> getAreaNameByDevId(long id) {
		QueryWrapper<EquipDevAreaDefEntity> queryWrapper = new QueryWrapper<EquipDevAreaDefEntity>();
		queryWrapper.eq("row_status",1);
		queryWrapper.eq("dev_id", id);
		List<EquipDevAreaDefEntity> selectList = equipDevAreaDefDao.selectList(queryWrapper);
		if(selectList != null && selectList.size()>0) {
			ArrayList<EquipDevAreaDef> arrayList = new ArrayList<EquipDevAreaDef>();
			for(EquipDevAreaDefEntity item:selectList) {
				EquipDevAreaDef equipDevAreaDef = new EquipDevAreaDef();
				equipDevAreaDef.setInnerData(item);
				arrayList.add(equipDevAreaDef);
			}
			return arrayList;
		}
		return null;
	}
}
